package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.SetmealDish;
import com.sky.enumeration.OperationType;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorsMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.service.SetmealService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorsMapper dishFlavorsMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private SetmealService setmealService;

    /*
    * 新增菜品
    * */
    @Override
    public void addDishWithFlavors(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        //添加菜品信息
        dishMapper.insertDish(dish);

        //添加口味信息
        /*主键回显*/
        Long id = dish.getId();

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(id);
            }

            dishFlavorsMapper.insertFlavors(flavors);
        }
    }

    /*
    * 菜品分页查询
    * */
    @Override
    public PageResult findAllByPage(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.selectAllByPage(dishPageQueryDTO);
        long total = page.getTotal();
        List<DishVO> records = page.getResult();
        return  new PageResult(total, records);
    }

    /*
    * 批量删除菜品
    * */
    @Transactional
    @Override
    public void deleteBatchDish(List<Long> ids) {
        //判断菜品是否为起售状态
        for (Long id : ids) {
            Dish dish = dishMapper.selectDishById(id);
            Integer status = dish.getStatus();
            if(status == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        //判断菜品是否与套餐相关联
        List<Long> setmealIds = setmealDishMapper.selectDishIdByDishIds(ids);
        if(setmealIds != null && setmealIds.size() > 0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        //删除菜品基本信息和口味信息
        dishMapper.deleteDishByIds(ids);
        dishFlavorsMapper.deleteDishFlavorsByIds(ids);
    }

    /*
    * 根据id查询菜品信息
    * */
    @Override
    public DishVO findDishById(Long id) {
        //查询菜品信息
        Dish dish = dishMapper.selectDishById(id);
        //查询菜品口味信息
        List<DishFlavor> flavors = dishFlavorsMapper.selectDishFlavorsByDishId(id);

        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        dishVO.setFlavors(flavors);
        return dishVO;
    }

    /*
    * 修改菜品信息
    * */
    @Transactional
    @Override
    public void updateDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);

        //修改菜品基本信息
        dishMapper.updateDish(dish);

        //删除菜品口味信息
        List<Long> dishFlavorList = new ArrayList<>();
        dishFlavorList.add(dishDTO.getId());
        dishFlavorsMapper.deleteDishFlavorsByIds(dishFlavorList);

        //新增菜品口味信息
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishDTO.getId());
            }
            dishFlavorsMapper.insertFlavors(flavors);
        }
    }

    /*
    * 修改菜品状态
    * */
    @Override
    public void updateDishStatus(Integer status, Long id) {

        Dish dish = dishMapper.selectDishById(id);
        dish.setStatus(status);

        dishMapper.updateDish(dish);

        // 当菜品停售时，关联停售包含此菜品的套餐
        if (status == StatusConstant.DISABLE) {
            // 查询包含此菜品的套餐ID列表
            List<SetmealDish> setmealDishes = setmealDishMapper.selectSetmealDishByDishId(id);
            List<Long> setmealIds = new ArrayList<>();
            for (SetmealDish setmealDish : setmealDishes) {
                setmealIds.add(setmealDish.getSetmealId());
            }

            if (setmealIds != null && !setmealIds.isEmpty()) {
                // 批量更新套餐状态为停售
                setmealService.updateSetmealStatusBatch(StatusConstant.DISABLE, setmealIds);
            }
        }
    }

    /*
    * 根据id分类查询菜品
    * */
    @Override
    public List<Dish> listByCategoryId(Long categoryId) {
        List<Dish> dishes = dishMapper.selectDishByCategoryId(categoryId);
        return dishes;
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.selectDishByCategoryId(dish.getCategoryId());

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorsMapper.selectDishFlavorsByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
